package com.cly.pay.controller;

import com.cly.common.entity.order.po.Order;
import com.cly.common.entity.pay.dto.PayDTO;
import com.cly.common.entity.pay.dto.PayOrderFormDto;
import com.cly.common.entity.pay.po.PayOrder;
import com.cly.common.entity.result.Result;
import com.cly.common.exception.CommonException;
import com.cly.pay.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pay")
@Slf4j
public class PayController {

    @Autowired
    private PayService payService;

    /**
     * 根据订单id查询支付订单返回给Order微服务
     * @param orderId
     * @return
     */
    @GetMapping("/private/{orderId}")
    public PayOrder getPayOrderByOrderIdToOrder(@PathVariable Long orderId){
        PayOrder payOrder = payService.lambdaQuery().eq(PayOrder::getOrderId, orderId).one();
        return payOrder;
    }

    /**
     * 创建支付订单
     * @param payDTO
     * @return
     */
    @PostMapping("/create")
    public Result createPayOrder(@RequestBody PayDTO payDTO){
        return payService.createPayOrder(payDTO);
    }

    /**
     * 根据订单id查询支付订单返回给前端
     * @param orderId
     * @return
     */
    @GetMapping("{orderId}")
    public Result getPayOrderByOrderId(@PathVariable Long orderId){
        PayOrder payOrder = payService.lambdaQuery().eq(PayOrder::getOrderId, orderId).one();
        return Result.success(payOrder);
    }

    /**
     * 尝试支付
     * @param payOrderFormDto
     */
    @PutMapping()
    public void tryPay(@RequestBody PayOrderFormDto payOrderFormDto){
        if(payOrderFormDto.getPayType() != 4){
            // 说明不是余额支付，目前只支持余额支付
            throw new CommonException("抱歉，目前只支持余额支付");
        }
        payService.tryPay(payOrderFormDto);
    }
}
